package course

import (
	"context"
	"github.com/jinzhu/copier"
	"xialou_platform/app/respx"
	"xialou_platform/services/agent/rpc/agent"

	"xialou_platform/services/agent/api/internal/svc"
	"xialou_platform/services/agent/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CourseListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCourseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) CourseListLogic {
	return CourseListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CourseListLogic) CourseList(req *types.CourseListReq) (resp *respx.PageResp, err error) {
	list, err := l.svcCtx.AgentRpc.CourseList(l.ctx, &agent.ListReq{
		Page:     req.Page,
		PageSize: req.PageSize,
		Keyword:  req.Keyword,
		Online:   req.Online,
	})
	if err != nil {
		return nil, err
	}
	courseList := make([]*types.CourseListResp, 0)

	for _, course := range list.List {
		temp := &types.CourseListResp{}
		copier.Copy(&temp, course)
		temp.Id = respx.IDFrom(course.Id).ToString()
		if len(course.Content) > 0 {
			temp.IsOpen = true
		}
		courseList = append(courseList, temp)
	}
	return respx.NewPageResp(req.Page, req.PageSize, list.Total, courseList), nil
}
